home *** CD-ROM | disk | FTP | other *** search
/ HPAVC / HPAVC CD-ROM.iso / ABERMUD.ZIP / GMAIN2.C < prev    next >
C/C++ Source or Header  |  1989-07-08  |  13KB  |  553 lines

  1. #include "files.h"
  2. #include <stdio.h>
  3. #include <sys/types.h>
  4. #include <sys/stat.h>
  5. #include "System.h"
  6.  
  7.  
  8. char lump[256];
  9. int namegiv=0;
  10. char namegt[80];
  11. int qnmrq=0;
  12.  
  13.  
  14.  
  15. /*
  16.  *        Program starts Here!
  17.  *
  18.  *    This forms the main loop of the code, as well as calling
  19.  *    all the initialising pieces
  20.  */
  21.  
  22.  
  23. void main(argc,argv)     /* The initial routine */
  24. int argc;
  25. char *argv[];
  26.     {
  27.     extern long ttyt;
  28.     long r;
  29.     FILE *a;
  30.     char user[80],b[40],space[400];
  31.     int num;
  32.     struct stat statbuf;
  33.     long ct;
  34.     
  35. /*
  36.  *    Check we are running on the correct host
  37.  *    see the notes about the use of flock();
  38.  *    and the affects of lockf();
  39.  */
  40.     gethostname(user,33);
  41.     if(strcmp(user,HOST_MACHINE))
  42.     {
  43.         fprintf(stderr,"AberMUD is only available on %s, not on %s\n",
  44.                 HOST_MACHINE,user);
  45.         exit(1);
  46.     }
  47.     b[0]=0;b[1]=0;b[2]=0;
  48. /*
  49.  *    
  50.  *        Check if there is a no logins file active
  51.  *
  52.  *
  53.  */
  54.     printf("\n\n\n\n");
  55.     chknolog();
  56.     if ((argc==2)&&(argv[1][0]=='-'))
  57.        {
  58.        uppercase(argv[1]);
  59. /*
  60.  *    Now check the option entries
  61.  *
  62.  *        -n(name)
  63.  */
  64.        r=argv[1][1];
  65.        switch(r)
  66.           {
  67.           case 'N':
  68.              qnmrq=1;ttyt=0;strcpy(namegt,argv[1]+2);namegiv=1;break;
  69.           default:
  70.              getty();
  71.              }
  72.        }
  73.     else
  74.        getty();
  75.     num=0;
  76. /*
  77.  *    Check for all the created at stuff
  78.  *
  79.  *    We use stats for this which is a UN*X system call
  80.  *
  81.  */
  82.  
  83.     if(!namegiv){
  84.        if(stat(EXE,&statbuf)== -1)strcpy(space,"<unknown>\n");
  85.        else
  86.        {
  87.                strcpy(space,ctime(&(statbuf.st_mtime)));
  88.        }
  89.        cls();
  90.        printf("\
  91.                          A B E R  M U D\n");printf("\
  92.                   By Alan Cox, Richard Acott Jim Finnis\n\n");
  93.        printf("This AberMUD was created:%s",space);
  94.        a=fopen(RESET_N,"r");
  95.        if(a==NULL) 
  96.        {
  97.               printf("AberMUD has yet to ever start!!!\n");
  98.               goto skip;
  99.            }
  100.        fscanf(a,"%ld",&r);
  101.        fclose(a);
  102.        time(&ct);
  103.        r=ct-r;
  104. /*
  105.  *
  106.  *    Elapsed time and similar goodies
  107.  *
  108.  */
  109.      
  110.        if(r>24*60*60) 
  111.     {
  112.         printf("Game time elapsed: Over a day!!!\n"); /* Add a Day ! */
  113.         goto skip;
  114.         }        
  115.        printf("Game time elapsed: ");
  116.        if(r<61) goto ski2;
  117.        if(r==60) {printf("1 minute\n");goto skip;};
  118.        if(r<120){printf("1 minute and ");goto ski2;}
  119.        if(r/60==60){printf("1 hour\n");goto skip;}
  120.        if(r<3600) {printf("%d minutes and ",r/60);goto ski2;}
  121.        if(r<7200) printf("1 hour and ");
  122.        else
  123.           printf("%d hours and ",r/3600);
  124.        if((r/60)%60!=1) printf("%d minutes.\n",(r/60)%60);
  125.        else
  126.           printf("1 minute\n");
  127.        goto skip;
  128.        ski2:if(r%60==1) printf("1 second\n");
  129.        else
  130.           printf("%d seconds.\n",r%60);
  131.        }
  132.     skip:login(user);                  /* Does all the login stuff */
  133.     if(!qnmrq)
  134.        {
  135.            cls();
  136.            listfl(MOTD);             /* list the message of the day */
  137.            gets(space);
  138.            printf("\n\n");
  139.        }
  140.     cuserid(space);
  141.     syslog("Game entry by %s : UID %s",user,space); /* Log entry */
  142.     talker(user);                /* Run system */
  143.     crapup("Bye Bye");                /* Exit */
  144.     }
  145.  
  146. char usrnam[44];
  147.  
  148.  
  149. void login(user)     /* The whole login system is called from this */
  150.  char *user;
  151.     {
  152.     long un1;
  153.     char usermc[80],a[80],tim[80],dat[80],c;
  154. /*
  155.  *
  156.  *    Check if banned first
  157.  *
  158.  */    
  159.     chkbnid(cuserid(NULL));
  160.  /*
  161.   *    Get the user name
  162.   *
  163.   */
  164.     if(!namegiv)
  165.        {
  166.        rena:printf("By what name shall I call you ?\n*");
  167.        getkbd(user,15);
  168.        }
  169.     else
  170.        strcpy(user,namegt);
  171. /*
  172.  *    Check for legality of names
  173.  *
  174.  */       
  175.     namegiv=0;
  176.     if (!strlen(user)) goto rena;
  177.     if (any('.',user)>-1) crapup("\nIllegal characters in user name\n");
  178.     trim(user);
  179.     scan(user,user,0," ","");
  180.     if (!strlen(user)) goto rena;
  181.     chkname(user);
  182.     if(!strlen(user)) goto rena;
  183.     strcpy(dat,user);             /* Gets name tidied up */
  184.     strcpy(usrnam,user);
  185.     if(!validname(usrnam)) crapup("Bye Bye");
  186.     if (logscan(dat,a)== -1)       /* If he/she doesnt exist */
  187.        {
  188.        printf("\nDid I get the name right %s ?",user);
  189.        gets(a);lowercase(a);c=a[0];
  190.        if (c=='n')  {printf("\n");goto rena;}  /* Check name */
  191.        }
  192.     logpass(user);        /* Password checking */
  193.     }
  194.  
  195. int chkbnid(user)   /* Check to see if UID in banned list */
  196.  char *user;
  197.     {
  198.     FILE *a;
  199.     char b[80],c[40];
  200.     extern char *strchr();
  201.     strcpy(c,user);
  202.     lowercase(c);
  203.     a=openlock(BAN_FILE,"r+");
  204.     if(a==NULL) return(0);
  205.     while (fgets(b,79,a)!=0)
  206.        {
  207.        if(strchr(b,'\n')) *strchr(b,'\n')=0;
  208.        lowercase(b);
  209.        if (strcmp(b,user)==0)
  210.           {
  211.           crapup("I'm sorry- that userid has been banned from the Game\n");
  212.           }
  213.        }
  214.     fclose(a);
  215.     }
  216.  
  217.  
  218.  
  219. long  logscan(uid,block)     /* Return block data for user or -1 if not exist */
  220.  char *uid;
  221.  char *block;
  222.     {
  223.     FILE *unit;
  224.     long f;
  225.     extern char lump[];
  226.     char wkng[128],wk2[128];
  227.     strcpy(wk2,uid);
  228.     unit=openlock(PFL,"r");f=0;
  229.     if(unit==NULL) crapup("No persona file\n");
  230.     while((f==0)&&(fgets(block,255,unit)!=0))
  231.        {
  232.        dcrypt(block,lump,strlen(block));
  233.        strcpy(block,lump);
  234.        scan(wkng,block,0,"",".");
  235.        if (strcmp(lowercase(wkng),lowercase(wk2))==0)f=1;
  236.        }
  237.     fclose(unit);
  238.     if (f==0) return(-1);
  239.     return(1);
  240. }
  241.  
  242. void logpass(uid)  /* Main login code */
  243.  char *uid;
  244.  {
  245.     long a,tries,b;
  246.     char pwd[32],sigf[128],pvs[32],block[128];
  247.     FILE *fl;
  248.     a=logscan(uid,block);
  249.     strcpy(pwd,uid); /* save for new user */
  250.     if (a==1)
  251.        {
  252.        a=scan(uid,block,0,"",".");
  253.        a=scan(pwd,block,a+1,"",".");
  254.        tries=0;
  255.        pastry:printf("\nThis persona already exists, what is the password ?\n*");
  256.        fflush(stdout);
  257.        gepass(block);
  258.        printf("\n");
  259.        if (strcmp(block,pwd))
  260.           {
  261.           if (tries<2) {tries++;goto pastry;}
  262.           else
  263.              crapup("\nNo!\n\n");
  264.           }
  265.        }
  266.     else
  267.        /* this bit registers the new user */
  268.        {
  269.         printf("Creating new persona...\n");
  270.         printf("Give me a password for this persona\n");
  271.         repass:printf("*");fflush(stdout);
  272.             gepass(block);
  273.             printf("\n");
  274.             if (any('.',block)!= -1)
  275.                 {
  276.                     printf("Illegal character in password\n");
  277.                     goto repass;
  278.                 }
  279.             if (!strlen(block)) goto repass;
  280.             strcpy(uid,pwd);
  281.             strcpy(pwd,block);
  282.             sprintf(block,"%s%s%s%s",uid,".",pwd,"....");
  283.               fl=openlock(PFL,"a");
  284.             if(fl==NULL) 
  285.              {
  286.             crapup("No persona file....\n");
  287.                 return;
  288.                }
  289.            qcrypt(block,lump,strlen(block));
  290.            strcpy(block,lump);
  291.            fprintf(fl,"%s\n",block);
  292.            fclose(fl);
  293.        }
  294.     cls();
  295.     }
  296.  
  297.  
  298.  
  299. void getunm(name)
  300.  char *name;
  301.     {
  302.     printf("\nUser Name:");
  303.     gets(name);
  304.     }
  305.  
  306. void showuser()
  307.     {
  308.     long a;
  309.     char name[80],block[256];
  310.     cls();
  311.     getunm(name);
  312.     shu(name,block);
  313.     printf("\nHit Return...\n");
  314.     while(getchar()!='\n');
  315.     }
  316.  
  317. long shu(name,block)  /* for show user and edit user */
  318.  char *name,*block;
  319.     {
  320.     long a;
  321.     long x;
  322.     char nm[128],pw[128],pr[128],pv[128];
  323.     a=logscan(name,block);
  324.     if (a== -1) printf("\nNo user registered in that name\n\n\n");
  325.     else
  326.        {
  327.        printf("\n\nUser Data For %s\n\n",name);
  328.        x=scan(nm,block,0,"",".");
  329.        x=scan(pw,block,x+1,"",".");
  330.        printf("Name:%s\nPassword:%s\n",nm,pw);
  331.        }
  332.     return(a);
  333.     }
  334.  
  335. void deluser()
  336. {
  337.     long a;
  338.     char name[80],block[256];
  339.     getunm(name);
  340.     a=logscan(name,block);
  341.     if (a== -1) printf("\nCannot delete non-existant user\n");
  342.     else
  343.     {
  344.     delu2(name);
  345.     }
  346. }
  347.  
  348. void edituser()
  349.     {
  350.     long a;
  351.     FILE *fl;
  352.     char name[80],block[256],bk2[256];
  353.     char nam2[128],pas2[128],per2[128],pr2[128];
  354.     cls();
  355.     getunm(name);
  356.     a=shu(name,block);
  357.     if (a== -1) sprintf(block,"%s%s",name,".default.E..");
  358.     a=scan(nam2,block,0,"",".");
  359.     a=scan(pas2,block,a+1,"",".");
  360.     printf("\nEditing : %s\n\n",name);
  361.     ed_fld("Name:",nam2);
  362.     ed_fld("Password:",pas2);
  363.     sprintf(bk2,"%s%s%s%s%s%s%s%s",nam2,".",pas2,".",".",".",".",".");
  364.     delu2(name);
  365.     fl=openlock(PFL,"a");
  366.     if(fl==NULL) return;
  367.     qcrypt(bk2,lump,strlen(bk2));
  368.     strcpy(bk2,lump);
  369.     fprintf(fl,"%s\n",bk2);
  370.     fclose(fl);
  371.     }
  372.  
  373. void ed_fld(name,string)
  374.  char *name,*string;
  375.     {
  376.     char bk[128];
  377.     bafld:printf("%s(Currently %s ):",name,string);
  378.     gets(bk);
  379.     if(bk[0]=='.') strcpy(bk,"");
  380.     if(strchr(bk,'.')){printf("\nInvalid Data Field\n");goto bafld;}
  381.     if (strlen(bk)) strcpy(string,bk);
  382.     }
  383. void delu2(name)   /* For delete and edit */
  384.  char *name;
  385.     {
  386.     char b2[128],buff[128];
  387.     FILE *a;
  388.     FILE *b;
  389.     char b3[128];
  390.     a=openlock(PFL,"r+");
  391.     b=openlock(PFT,"w");
  392.     if(a==NULL) return;
  393.     if(b==NULL) return;
  394.     while(fgets(buff,128,a)!=0)
  395.        {
  396.        dcrypt(buff,lump,strlen(buff)-1);
  397.        scan(b2,lump,0,"",".");
  398.        strcpy(b3,name);lowercase(b3);
  399.        if (strcmp(b3,lowercase(b2))) fprintf(b,"%s",buff);
  400.        }
  401.     fclose(a);
  402.     fclose(b);
  403.     a=openlock(PFL,"w");
  404.     b=openlock(PFT,"r+");
  405.     if(a==NULL) return;
  406.     if(b==NULL) return;
  407.     while(fgets(buff,128,b)!=0)
  408.        {
  409.        fprintf(a,"%s",buff);
  410.        }
  411.     fclose(a);
  412.     fclose(b);
  413.     }
  414.  
  415.   
  416. void chpwd(user)   /* Change your password */
  417.  char *user;
  418.     {
  419.     char block[128],data[128],pwd[80],pv[80];
  420.     long a;
  421.     FILE *fl;
  422.     strcpy(data,user);
  423.     logscan(user,block);
  424.     strcpy(user,data);
  425.     a=scan(data,block,0,"",".");
  426.     a=scan(pwd,block,a+1,"",".");
  427.     printf("\nOld Password\n*");
  428.     fflush(stdout);
  429.     gepass(data);
  430.     if(strcmp(data,pwd)) printf("\nIncorrect Password\n");
  431.     else
  432.        {
  433.        printf("\nNew Password\n");
  434.        chptagn:printf("*");
  435.        fflush(stdout);
  436.        gepass(pwd);
  437.        printf("\n");
  438.        if (!strlen(pwd)) goto chptagn;
  439.        if (strchr(pwd,',')) 
  440.     {
  441.         printf("Illegal Character in password\n");
  442.         goto chptagn;
  443.     }
  444.        printf("\nVerify Password\n*");
  445.        gepass(pv);
  446.        printf("\n");
  447.        if (strcmp(pv,pwd))
  448.        {
  449.         printf("\nNO!\n");
  450.         goto chptagn;
  451.     }
  452.        sprintf(block,"%s%s%s%s%s%s%s%s",user,".",pwd,".",".",".",".",".");
  453.        delu2(user);  /* delete me and tack me on end! */
  454.        fl=openlock(PFL,"a");
  455.        if(fl==NULL) return;
  456.        qcrypt(block,lump,strlen(block));
  457.        strcpy(block,lump);
  458.        fprintf(fl,"%s\n",block);
  459.        fclose(fl);
  460.        printf("Changed\n");
  461.    }
  462. }
  463.  
  464.  
  465. char *getkbd(s,l)   /* Getstr() with length limit and filter ctrl */
  466.  char *s;
  467.  int l;
  468.     {
  469.     char c,f,n;
  470.     f=0;c=0;
  471.     while(c<l)
  472.        {
  473.        regec:n=getchar();
  474.        if ((n<' ')&&(n!='\n')) goto regec;
  475.        if (n=='\n') {s[c]=0;f=1;c=l-1;}
  476.        else
  477.           s[c]=n;
  478.        c++;
  479.        }
  480.     if (f==0) {s[c]=0;while(getchar()!='\n');}
  481.     return(s);
  482.     }
  483.     
  484.  
  485.  
  486. void listfl(name)
  487.  char *name;
  488.     {
  489.     FILE * unit;
  490.     char string[82];
  491.     printf("\n");
  492.     unit=openlock(name,"r+");
  493.     if (unit==NULL) 
  494.     {
  495.         printf("[Cannot Find -> %s]\n",name);
  496.         return;
  497.     }
  498.     while(fgets(string,128,unit)!=0)
  499.        {
  500.        printf("%s",string);
  501.        }
  502.     fclose(unit);
  503.     printf("\n");
  504.     }
  505.  
  506. void crapup(ptr)
  507.  char *ptr;
  508.     {
  509.     printf("\n%s\n\nHit Return to Continue...\n",ptr);
  510.     gets(ptr);
  511.     exit();
  512.     }
  513.  
  514. /*
  515.  *        This is just a trap for debugging it should never get
  516.  *        called.
  517.  */ 
  518.  
  519. void bprintf()
  520. {
  521.     printf("EEK - A function has trapped via the bprintf call\n");
  522.     exit(0);
  523. }
  524.  
  525. int chkname(user)
  526. char *user;
  527. {
  528. long a;
  529. a=0;
  530. lowercase(user);
  531. while(user[a])
  532. {
  533. if(user[a]>'z') {user[a]=0;return(-1); }
  534. if(user[a]<'a') {user[a]=0;return(-1);}
  535. a++;
  536. }
  537. user[0]-=32;
  538. return(0);
  539. }
  540. void chknolog()
  541. {
  542. FILE *a;
  543. char b[128];
  544. a=fopen(NOLOGIN,"r");
  545. if(a==NULL) return;
  546. while(fgets(b,128,a))
  547. {
  548. printf("%s",b);
  549. }
  550. fclose(a);
  551. exit(0);
  552. }
  553.